package com.android.drm;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

import com.android.drm.to.User;
import com.android.drm.help.CustomDialog;

/**
 * <p><b>CLASS:</b> com.android.drm.HomeScreen</p>
 *
 * ...........................................................................
 * <p><b>DESCRIPTION:</b><br/>
 * Home screen for the application.
 * </p>
 * 
 * ...........................................................................
 * <p><b>MODIFICATION HISTORY:</b>
 * <p>[2010/08/16] michellem: Initial version.</p>
 * </p>
 * 
 * ...........................................................................
 * @author michellem
 * @since 1.0
 */

public class HomeScreen extends Activity implements AdapterView.OnItemSelectedListener {
	
	private DRMApplication drmApplication;
	private Button mTestsButton;
	private Button mReportsButton;
	private Button mChartsButton;
	private Button mSetupButton;
	private Button mHelpButton;
	private Button mExitButton;
	private Spinner mUserName;	
	private List<User> userList;
	private long userId;
	private String userName;
	private long actionId;
	private String actionName;
	private static final int HELP_DIALOG = 1;
	private static final int ABOUT_DIALOG = 2;
	private boolean userChanged = true;
	private String userC;
	private long userCl = 0;	
	private String myString = "first";

	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_screen);
        drmApplication = (DRMApplication) this.getApplication();   
        /*
        if (savedInstanceState != null) {
        	Toast.makeText(this, "onCreate: " + 
        		savedInstanceState.getString("MyString"), Toast.LENGTH_SHORT).show();
        } */
        setWidget();
        addUsernameAdapter();
    }
    
    public void onResume() {
        super.onResume();
        //addUsernameAdapter();
        
        //getIntentExtras();

        /*
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
        	userCl = bundle.getLong("userCl");
        }
        */
             
        //if (userChanged == true) {
        //if (userC == "true") {
        /*
        if (userCl == 1) {
        	addUsernameAdapter();
        }  else {
        	Toast.makeText(this, "userC is null",
					Toast.LENGTH_SHORT).show();
        } */
    }
    
    /*
    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
    	// Save UI state changes to the savedInstanceState.
    	// This bundle will be passed to onCreate if the process is
    	// killed and restarted.
    	savedInstanceState.putString("MyString", "Welcome back to Android");
    	super.onSaveInstanceState(savedInstanceState);
    	Toast.makeText(this, "onSave", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
      super.onRestoreInstanceState(savedInstanceState);
      myString = savedInstanceState.getString("MyString");
      Toast.makeText(this, "onRestore" + myString, Toast.LENGTH_SHORT).show();
    }
    */
   
	private void getIntentExtras() {
		Intent i = getIntent();
		Bundle b = i.getExtras();
		//this.userChanged = b.getBoolean("userChanged");
		//this.userC = b.getString("userC");
		this.userCl = b.getLong("userCl");
	}
					
	public void myClickHandler(View v) {
		if (v.getId() == R.id.helpButton) {
			HomeScreen.this.showDialog(HELP_DIALOG);		
		} 
		else if (v.getId() == R.id.exitButton) {
			finish();
		} 
		else if (v.getId() == R.id.setupButton) {
			startActivity(new Intent(this, SetupSelection.class));
		}		
		else if (v.getId() == R.id.alarmButton) {
			Toast.makeText(this, "Not implemented.", Toast.LENGTH_SHORT).show();
		}
		else if (v.getId() == R.id.logo) {
			HomeScreen.this.showDialog(ABOUT_DIALOG);
		}
		else { 
			if (userId == 0) {
				Toast.makeText(this, "Please select a user!\nTo create new user, go to Setup.",
						Toast.LENGTH_SHORT).show();
				return;
			}

			Intent intent = null;		
			switch (v.getId()) {
			case R.id.testsButton:
				actionId = 1;
				actionName = "Four Of Nine";
				intent = new Intent(this, FourOfNine.class);
				break;
			case R.id.chartsButton:
				intent = new Intent(this, ChartSelection.class);
				break;
			default:
				break;
			}

			if (intent != null) {
				setIntentExtras(intent);
				startActivity(intent);
			}
		}
	}
	
	
	private void setIntentExtras(Intent intent) {
		intent.putExtra("userId", userId);
		intent.putExtra("userName", userName);
		intent.putExtra("actionId", actionId);
		intent.putExtra("actionName", actionName);
	}
    
	public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
		if (parent.getId() == findViewById(R.id.userName).getId()) {
			userName = (String) mUserName.getSelectedItem();
			userId = position == 0 ? 0 : userList.get(position - 1).getId();
		}
	}
    
    public void onNothingSelected(AdapterView<?> parent){
    	// Don't have anything to do.
    }
    
    public Dialog onCreateDialog(int dialogId) {
    	Dialog dialog = null;
    	switch (dialogId) {
	    	case HELP_DIALOG :
				CustomDialog.Builder customBuilder = new
				CustomDialog.Builder(HomeScreen.this);
				customBuilder.setMessage(R.string.HomeHelpMsg)
					.setPositiveButton("Close", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					});
	            dialog = customBuilder.create();
	    		break;
	    	case ABOUT_DIALOG : 
				CustomDialog.Builder aboutBuilder = new
				CustomDialog.Builder(HomeScreen.this);
				aboutBuilder.setMessage(R.string.AboutMsg)
					.setPositiveButton("Close", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					});
	            dialog = aboutBuilder.create();
	    		break;
    	}
    	return dialog;
    }
   
    private void setWidget() {
    	mTestsButton = (Button) this.findViewById(R.id.testsButton);
    	mReportsButton = (Button) this.findViewById(R.id.alarmButton);
    	mChartsButton = (Button) this.findViewById(R.id.chartsButton);
    	mSetupButton = (Button) this.findViewById(R.id.setupButton);
    	mHelpButton = (Button) this.findViewById(R.id.helpButton);
    	mExitButton = (Button) this.findViewById(R.id.exitButton); 
    	mUserName = (Spinner) this.findViewById(R.id.userName);
    }
    
	private void addUsernameAdapter() {
		mUserName.setOnItemSelectedListener(this);
		ArrayAdapter<String> aa = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, getUserNames());
		aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mUserName.setAdapter(aa);
	}

	private String[] getUserNames() {
		userList = this.drmApplication.getDataHelper().selectUsers();
		String[] userNames = new String[userList.size() + 1];
		userNames[0] = getString(R.string.select_user);
		for (int i = 0; i < userList.size(); i++) {
			userNames[i + 1] = userList.get(i).getUserName();
		}
		return userNames;
	}
    
	//added comment
}
